package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.pojo.product.Product;
import com.atguigu.spzx.model.request.product.ProductQueryRequest;
import com.atguigu.spzx.model.vo.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author dyl
 * @version 1.0
 * @description:
 * @date 2025/1/9 16:09
 */
@RestController
@RequestMapping("/admin/product/product")
@Tag(name = "商品管理")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/findPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<Page<Product>> findPage(@PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize") Integer pageSize,
                                          ProductQueryRequest req) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        Page<Product> productPage = productService.findPage(page,req);
        return Result.ok(productPage);
    }

    @PostMapping("/saveProduct")
    @Operation(summary = "添加商品")
    public Result saveProduct(@RequestBody Product product){
        productService.saveProduct(product);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id获取商品")
    public Result<Product> saveProduct(@PathVariable("id")Long id){
        return Result.ok(productService.getProductById(id));
    }

    @PutMapping("/updateProduct")
    @Operation(summary = "修改商品")
    public Result updateProduct(@RequestBody Product product){
        productService.updateProduct(product);
        return Result.ok();
    }

    @DeleteMapping("/deleteProductById/{id}")
    @Operation(summary = "根据id删除商品")
    public Result deleteProductById(@PathVariable("id")Long id){
        productService.deleteProductById(id);
        return Result.ok();
    }

    @PutMapping("/takeonProduct/{id}")
    @Operation(summary = "商品上架")
    public Result takeonProduct(@PathVariable("id")Long id){
        productService.takeonProduct(id);
        return Result.ok();
    }


    @PutMapping("/takeoffProduct/{id}")
    @Operation(summary = "商品下架")
    public Result takeoffProduct(@PathVariable("id")Long id){
        productService.takeoffProduct(id);
        return Result.ok();
    }

    @PutMapping("/auditProduct/{id}/{auditStatus}")
    @Operation(summary = "审核商品")
    public Result auditProduct(@PathVariable("id")Long id,
                               @PathVariable("auditStatus")Integer auditStatus){
        productService.auditProduct(id,auditStatus);
        return Result.ok();
    }

}
